﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericHtml
{
    public class NameHelper
    {
        //public static readonly string[] Ignores = new string[] 
        //{ 
        //    "page", 
        //    "rows",
        //    "id",
        //    "creationtime",
        //    "createuser",
        //    "updatedtime",
        //    "updateuser",
        //    "__rowid"
        //};

        public static readonly HashSet<string> Ignores =
            new HashSet<string>(StringComparer.OrdinalIgnoreCase)
        { 
            "page", 
            "rows",
            "id",
            "creationtime",
            "createuser",
            "updatedtime",
            "updateuser",
            "__rowid"
        };


        /// <summary>
        /// 字母开头， 数字或下划线组成
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool CheckName(string input)
        {
            if (input == null || input.Length == 0 || input.Length > 20) return false;
            if (!IsEnChar(input[0])) return false;
            for (int i = 1; i < input.Length; i++)
            {
                if (!IsSupportChar(input[i]))
                    return false;
            }

            return true;
        }

        /// <summary>
        /// 全英文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool CheckColumnName(string input)
        {
            if (input == null || input.Length == 0 || input.Length > 20) return false;
            for (int i = 0; i < input.Length; i++)
            {
                if (!IsSupportChar(input[i]))
                    return false;
            }

            if (Ignores.Contains(input)) return false;

            return true;
        }

        public static bool IsEnChar(char ch)
        {
            if ((ch >= 'a' && ch <= 'z') ||
                (ch >= 'A' && ch <= 'Z'))
            {
                return true;
            }

            return false;
        }

        public static bool IsSupportChar(char ch)
        {
            if ((ch >= '0' && ch <= '9') ||
                ch == '_' ||
                IsEnChar(ch))
            {
                return true;
            }

            return false;
        }
    }
}
